<template>
	<div>
		<AplidForm ref="formRef" v-bind="ExtendFormConfig" v-model="formModel" @submit="onSubmit">
			<!-- <template v-slot:contactHtmlForm>
				<div class="node-label f-between">
					<span class="mr8">联系人信息</span>
					<el-button size="small" type="primary" @click="visibleForm = !visibleForm"> 批量采集 </el-button>
				</div>
			</template> -->
		</AplidForm>
		<el-drawer size="100%" v-model="visibleForm" title="批量采集" append-to-body :close-on-click-modal="false">
			<ImportForm></ImportForm>
		</el-drawer>
	</div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import ImportForm from '../../ImportForm/index.vue'
import { ExtendFormConfig } from '../../config'
import { getAreaObj } from '@/utils/util'
import { useCommonStore } from '@/stores/common'
const emit = defineEmits(['refresh', 'operate'])
const visibleForm = ref(false)
const formModel = ref({
	dp_service_object_contact: [],
	dp_service_object_material: [],
})
const formRef = ref(null)

const onSubmit = (form, done) => {
	const { tempNowObj } = storeToRefs(useCommonStore())
	const params = {
		dpServiceObjectMaterialPOs: [],
		dpServiceObjectExtendPO: {
			...form,
			dp_service_object_material: undefined,
			dp_service_object_contact: undefined,
			idCard: tempNowObj.value.idCard,
			code: 1003,
		},
		dpServiceObjectContactPOs: [],
	}
	if (form.dp_service_object_material && form.dp_service_object_material.length > 0) {
		params.dpServiceObjectMaterialPOs = form.dp_service_object_material.map((item) => {
			return {
				...item,
				idCard: tempNowObj.value.idCard,
				code: 1004,
			}
		})
	}
	if (form.dp_service_object_contact && form.dp_service_object_contact.length > 0) {
		params.dpServiceObjectContactPOs = form.dp_service_object_contact.map((item) => {
      if(item.contactCardId) item.contactCardId = item.contactCardId.toUpperCase()
			const areaObj: any = getAreaObj(item.contactAreas, 'contact')
			const areaNameObj: any = getAreaObj(item.contact_areas_name, 'contact')
			const data = {
				...item,
				...areaObj,
				...areaNameObj,
				idCard: tempNowObj.value.idCard,
				contactAreas: undefined,
				contact_areas_name: undefined,
				code: 1002,
			}
			return data
		})
	}
	apiMod
		.view_dpServiceObjectExtend_gatherAll(params)
		.then((res) => {
			emit('refresh', res.data)
			done()
		})
		.catch((err) => {
			done()
		})
}

const onSave = () => {
	formRef.value!.onSubmit()
}
defineExpose({
	onSave,
})
</script>

<style scoped></style>
